package com.yimi.yinhepda.views.offline;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.pda.ym.base.BaseActivity;
import com.pda.ym.base.BaseTitleActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.yimi.yinhepda.utils.Common;
import com.yimi.yinhepda.utils.ThreadPoolUtils;
import com.yimi.yinhepda.views.load_module.LoadUnloadGroupActivity;
import com.yimi.yinhepda.views.offline.load.OffLineLoadUtil;
import com.yimi.yinhepda.views.offline.load.OfflLneLoadActivity;
import com.yimi.yinhepda.views.offline.load.OffLineLoadCacheActivity;
import com.yimi.yinhepda.views.offline.load.OffLineLoadSuccessActivity;
import com.yimi.yinhepda.views.offline.unload.OffLineUnLoadCacheActivity;
import com.yimi.yinhepda.views.offline.unload.OffLineUnLoadUtil;
import com.yimi.yinhepda.views.offline.unload.OffLineUnloadCreateActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 离线操作目录
 *
 * @author lj
 */
public class OffLineOperatorListActivity extends BaseTitleActivity {

    /**
     * 装车暂存数量
     */
    private Button btnUnloadSaveNum;
    /**
     * 卸车暂存数量
     */
    private Button btnLoadSaveNum;
    public static Handler handler;

    @Override
    public String getPageName() {
        return "离线操作目录";
    }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onStart() {
        super.onStart();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                //显示离线操作数量
                showOfflineDataNum();
            }
        };
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_offline_operator_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnUnloadSaveNum = findViewById(R.id.btn_unload_temp_save);
        btnLoadSaveNum = findViewById(R.id.btn_load_temp_save);
        findViewById(R.id.btn_loadCar).setOnClickListener(this);
        findViewById(R.id.btn_unLoadCar).setOnClickListener(this);
        findViewById(R.id.btn_singleSealCar).setOnClickListener(this);
        findViewById(R.id.btn_unloadTaskList).setOnClickListener(this);
        findViewById(R.id.btn_loadTaskList).setOnClickListener(this);
        findViewById(R.id.btn_back).setOnClickListener(this);

        //上传离线装车数据
        OffLineLoadUtil.uploadOfflineLoadData();
        //上传离线卸车数据
        OffLineUnLoadUtil.uploadOfflineLoadData();

        ThreadPoolUtils.execute(new UploadRunnable());
    }

    @Override
    protected void onResume() {
        super.onResume();

        //显示离线操作数量
        showOfflineDataNum();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler = null;
    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }


    /**
     * 线程池执行耗时操作
     */
    private class UploadRunnable implements Runnable {
        @Override
        public void run() {
            //清除本地15天前的数据
            DbHelper.deleteData();
            //初始化车牌数据
            Common.initCarPalteData(OffLineOperatorListActivity.this);
        }
    }

    /**
     * 离线操作数量
     */
    private void showOfflineDataNum() {
        UnloadTaskTableDao unloadTaskTableDao = new UnloadTaskTableDao();
        String userCode = sharedPreferUtil.getValue(Common.USER_CODE);
        //装车任务暂存数据
        int loadCount = 0;
        List<UnloadTaskTable> loadTaskList = unloadTaskTableDao.findByOperTypeCode1(userCode, sharedPreferUtil.getDeptCode());
        if (loadTaskList != null) {
            for (UnloadTaskTable item : loadTaskList) {
                if (item.getOfflineTaskNo() != null && item.getOfflineTaskNo().startsWith("Z") && item.getIsUpload() != 1) {
                    loadCount++;
                }
            }
        }
        if (loadCount == 0) {
            btnLoadSaveNum.setText("");
            btnLoadSaveNum.setVisibility(View.GONE);
        } else {
            btnLoadSaveNum.setText(loadCount + "");
            btnLoadSaveNum.setVisibility(View.VISIBLE);
        }

        //卸车任务暂存数据
        int unloadCount = 0;
        List<UnloadTaskTable> unloadTaskList = unloadTaskTableDao.findByOperTypeCode2(userCode, sharedPreferUtil.getDeptCode());
        if (unloadTaskList != null) {
            for (UnloadTaskTable item : unloadTaskList) {
                if (item.getOfflineTaskNo() != null && item.getOfflineTaskNo().startsWith("X") && item.getIsUpload() != 1) {
                    unloadCount++;
                }
            }
        }
        if (unloadCount == 0) {
            btnUnloadSaveNum.setText("");
            btnUnloadSaveNum.setVisibility(View.GONE);
        } else {
            btnUnloadSaveNum.setText(unloadCount + "");
            btnUnloadSaveNum.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_loadCar:
                //出发封车
                Intent intent;
                int deptType = sharedPreferUtil.getIntValue(Common.DEPT_TYPE);
                if (deptType == 5 || deptType == 6) {
                    //判断是否是分拨,跳转至装卸组
                    intent = new Intent(this, LoadUnloadGroupActivity.class);
                } else {
                    //跳转至离线装车界面
                    intent = new Intent(this, OfflLneLoadActivity.class);
                }

                Bundle bundle = new Bundle();
                //扫描类型：1-装车扫描，2-卸车扫描,3-离线装车扫描，4-离线卸车扫描
                bundle.putInt("scanType", 3);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.btn_unLoadCar:
                deptType = sharedPreferUtil.getIntValue(Common.DEPT_TYPE);
                if (deptType == 5 || deptType == 6) {
                    //判断是否是分拨,跳转至装卸组
                    intent = new Intent(this, LoadUnloadGroupActivity.class);
                } else {
                    //跳转至离线装车界面
                    intent = new Intent(this, OffLineUnloadCreateActivity.class);
                }

                bundle = new Bundle();
                //扫描类型：1-装车扫描，2-卸车扫描,3-离线装车扫描，4-离线卸车扫描
                bundle.putInt("scanType", 4);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.btn_singleSealCar:
                //单独封车
                intent = new Intent(this, OffLineLoadSuccessActivity.class);
                //装车类型：1-离线装车，2-离线卸车，3-单独封车
                intent.putExtra("loadType", 3);
                startActivity(intent);
                break;
            case R.id.btn_unloadTaskList:
                toActivity(OffLineUnLoadCacheActivity.class);
                break;
            case R.id.btn_loadTaskList:
                //装车暂存
                toActivity(OffLineLoadCacheActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            default:
                break;
        }
    }
}
